package com.liruo.learn.spring.mvc.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpMethod;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.servlet.Filter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.stream.Stream;

/**
 * @Author:liruo
 * @Date:2023-03-11-10:26:32
 * @Desc
 */
//@Configuration
@Slf4j
public class CorsConfiguration implements WebMvcConfigurer {
//    @Bean
//    public FilterRegistrationBean<CorsFilter> corsFilter(){
//        org.springframework.web.cors.CorsConfiguration  config = new org.springframework.web.cors.CorsConfiguration ();
//// Possibly...
//// config.applyPermitDefaultValues()
//        config.setAllowCredentials(true);
//        config.addAllowedOrigin("https://domain1.com");
//        config.addAllowedHeader("*");
//        config.addAllowedMethod("*");
//
//        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
//        source.registerCorsConfiguration("/**", config);
//
//        CorsFilter filter = new CorsFilter(source);
//        return new FilterRegistrationBean<>(filter);
//    }
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/cors/**")
                .allowedOrigins("http://localhost:3000")
                .allowedMethods("POST")
                .allowedHeaders("*")
                .allowCredentials(true)
                .maxAge(3600);

    }
    public Filter corsTestFilter = (req, resp, chain)->{
        HttpServletRequest sreq = (HttpServletRequest) req;
        if (sreq.getMethod().equalsIgnoreCase(HttpMethod.OPTIONS.name())) {
            log.info("Preflight Request:{}  Request Method: {} Request Headers: {} Request Headers: {}" ,
                    sreq.getHeader("Origin"),
                    sreq.getMethod(),
                    sreq.getHeader("Access-Control-Request-Headers"),
                    sreq.getHeader("Access-Control-Request-Method")
            );
        }
        chain.doFilter(req, resp);

        HttpServletResponse sresp = (HttpServletResponse) resp;
        if (sreq.getMethod().equalsIgnoreCase(HttpMethod.OPTIONS.name())) {
            log.info("Preflight Response:{}  Request Method: {} Request Headers: {} Request Headers: {}" ,
                    sresp.getHeader("Origin"),
                    sreq.getMethod(),
                    sresp.getHeader("Access-Control-Request-Headers"),
                    sresp.getHeader("Access-Control-Request-Method")
            );
        }
    };
    @Bean
    public FilterRegistrationBean<Filter> corsTestFilter(){
        FilterRegistrationBean<Filter> corsTestFilter = new FilterRegistrationBean<>();
        corsTestFilter.setFilter(this.corsTestFilter);
        corsTestFilter.setUrlPatterns(Stream.of("/*").toList());
        return corsTestFilter;
    }
}
